<?php
class Controller_Emailflow extends Controller_Abstract
{
	function actionIndex(){
		$emailModel = new Email();
		$this->_view['existsEvents'] = $emailModel->getEmailEvents();
		$this->_view['implementingEvents'] = $emailModel->getImplementEmailEvents();
		$this->_view['existsTemplates'] = $emailModel->getEmailTemplates();
		$this->_view['sites'] = DataUtil::getOptionsForSites();
	}
	
	function actionEmailevents()
	{
		if($this->_context->isPOST()){
			$eventName = $this->_context->post('event_name');
			$logic = $this->_context->post('logic');
			$condition = $this->_context->post('condition');
			$relation = $this->_context->post('relation');
			$conditionVal = $this->_context->post('condition_val');
			$templateId = $this->_context->post('template_id', '');
			
			$conditionArr = array();
			foreach($condition as $k => $v){
				$conditionArr[] = 	array( 
										'factor' => $v,
										'relation' => $relation[$k],
										'val' => $conditionVal[$k]
									);
			}
			
			$emailModel = new Email();
			$insertId = $emailModel->addEmailEvent($eventName, $logic, $conditionArr, $templateId);
			return json_encode($insertId);
		}
	}
	
	function actionEmaileventsmodify()
	{
		if($this->_context->isPOST()){
			$modifyId = $this->_context->post('modify_id', null);
			$eventName = $this->_context->post('event_name');
			$logic = $this->_context->post('logic');
			$condition = $this->_context->post('condition');
			$relation = $this->_context->post('relation');
			$conditionVal = $this->_context->post('condition_val');
			$templateId = $this->_context->post('template_id', '');
			
			$conditionArr = array();
			foreach($condition as $k => $v){
				$conditionArr[] = 	array( 
										'factor' => $v,
										'relation' => $relation[$k],
										'val' => $conditionVal[$k]
									);
			}
			
			$emailModel = new Email();
			$result = $emailModel->modifyEmailEvent($modifyId, $eventName, $logic, $conditionArr, $templateId);
			return json_encode($result);
		}
	}
	
	function actionEmaileventdelete()
	{
		if($this->_context->isPOST()){
			$eventId = $this->_context->post('event_id', null);
			
			$emailModel = new Email();
			$result = $emailModel->deleteEmailEventById($eventId);
			return json_encode($result);
		}
	}
	
	function actionEmaileventsactived()
	{
		if($this->_context->isPOST()){
			$activeArr = $this->_context->post('activeArr', array());
			$emailModel = new email();
			
			if(!$emailModel->setEmailActived($activeArr)){
				return 'err';
			}
			
			return 'ok';
		}
	}
	
	function actionEmaileventinfo()
	{
		$eventId = $this->_context->get('event_id', '');
		$emailModel = new email();
		
		$eventInfo = $emailModel->getEmailEventInfoById($eventId);
		return json_encode($eventInfo);
	}
	
	function actionAddtemplate()
	{
		$templateName = $this->_context->post('template_name');
		$templateSubject = $this->_context->post('template_subject');
		$templateContent = $this->_context->post('template_content');
		
		$emailModel = new email();
		$insertId = $emailModel->addEmailTemplate($templateName, $templateSubject, $templateContent);
		return json_encode($insertId);
	}
	
	function actionEmailtemplateinfo()
	{
		$templateId = $this->_context->get('template_id', '');
		$emailModel = new email();
		
		$templateInfo = $emailModel->getEmailTemplateInfoById($templateId);
		return json_encode($templateInfo);
	}
	
	function actionEmailtemplatemodify()
	{
		if($this->_context->isPOST()){
			$modifyId = $this->_context->post('template_id', null);
			$templateName = $this->_context->post('template_name');
			$templateSubject = $this->_context->post('template_subject');
			$eventIdArr = $this->_context->post('event_id_arr');
			$templateContent = $this->_context->post('template_content');
			
			$emailModel = new Email();
			$result = $emailModel->modifyEmailTemplate($modifyId, $templateName, $templateSubject, $templateContent, $eventIdArr);
			return json_encode($result);
		}
	}
	
	function actionEmailtemplatedelete()
	{
		if($this->_context->isPOST()){
			$templateId = $this->_context->post('template_id', null);
			
			$emailModel = new Email();
			$result = $emailModel->deleteEmailTemplateById($templateId);
			return json_encode($result);
		}
	}
	
	function actionTestsend(){
		$templateId = $this->_context->post('template_id', '');
		$sendto = $this->_context->post('sendto', '');
		
		$result = Util::addNewEmailFlow($templateId, 0, array('email'=>$sendto, 'priority'=>10000));
		return json_encode($result);
	}
	
}